HAL DAC Functions

Initialization, de-initialization, configuration, calibration functions

group DAC_Exported_Functions_Group1_1

This section provides functions allowing to:

  • Initialize the DAC,

  • De-initialize the DAC,

  • Configure the DAC,

  • Chose optimum frequency,

  • Calibration setting.

Functions

hal_status_t HAL_DAC_Init ( hal_dac_handle_t * hdac , hal_dac_t instance )

Initialize the DAC peripheral handle with a DAC instance.

Note

After calling this function, the DAC jumps to HAL_DAC_STATE_SEPARATE_CHANNEL_CONFIGURED, and it is possible to call directly HAL_DAC_StartCHannel() without calling HAL_DAC_SetConfig() or HAL_DAC_SetConfigChannel(). In this case the DAC default configuration parameters are:

  • no DMA linked with DAC

  • HAL_DAC_HIGH_FREQ_MODE_DISABLED

  • HAL_DAC_SAMPLE_AND_HOLD_DISABLED

  • HAL_DAC_AM_DISABLED

  • HAL_DAC_DMA_DOUBLE_DATA_MODE_DISABLED

  • HAL_DAC_DATA_ALIGN_12_BITS_RIGHT

  • HAL_DAC_SIGN_FORMAT_UNSIGNED

  • HAL_DAC_TRIGGER_NONE

  • HAL_DAC_OUTPUT_BUFFER_ENABLED

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • instance – A DAC hardware peripheral base address.

Return values :
  • HAL_OK – DAC instance has been correctly initialized.

  • HAL_INVALID_PARAM – A parameter is invalid.

void HAL_DAC_DeInit ( hal_dac_handle_t * hdac )

Deinitialize the DAC peripheral.

Note

Stop the DAC and restore the state machine to reset state.

Parameters :

hdac – Pointer to a hal_dac_handle_t structure.

hal_dac_high_freq_mode_t HAL_DAC_GetOptimumFrequencyMode ( const hal_dac_handle_t * hdac )

Get the optimum frequency interface mode for the DAC peripheral.

Note

After calling this function, set the optimum high frequency interface mode (HFSEL bits) with HAL_DAC_SetConfig().

Parameters :

hdac – Pointer to a hal_dac_handle_t structure.

Return values :

hal_dac_high_freq_mode_t – Optimum frequency interface mode.

hal_status_t HAL_DAC_SetConfig ( hal_dac_handle_t * hdac , const hal_dac_config_t * p_config )

Configure the DAC peripheral according to the specified parameters in the hal_dac_config_t.

Note

By calling this function, the high frequency interface mode (HFSEL bits) is set. Optionally, before calling this function, the optimum high frequency interface mode (HFSEL bits) could be determined with HAL_DAC_GetOptimumFrequencyMode().

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • p_config – The configuration that contains information for the specified DAC.

Return values :
  • HAL_OK – DAC instance has been correctly configured.

  • HAL_INVALID_PARAM – A parameter is invalid.

void HAL_DAC_GetConfig ( const hal_dac_handle_t * hdac , hal_dac_config_t * p_config )

Return the configuration parameters of the DAC peripheral in the hal_dac_config_t.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • p_config – The configuration parameters.

void HAL_DAC_ResetConfig ( hal_dac_handle_t * hdac )

Reset the configuration parameters of the DAC and its channels.

Note

Configuration parameters of the DAC are reset to:

  • DAC is disabled and no more DMA is running with DAC

  • normal mode operation (ie: not in calibration mode)

  • high frequency disabled

  • sample and hold mode is disabled

  • no wave generation

  • DMA double data mode is disabled

  • unsigned data

  • 12 bits right alignment

  • trigger none

  • output buffer enabled

  • external pin connection

Parameters :

hdac – Pointer to a hal_dac_handle_t structure.

hal_status_t HAL_DAC_CalibrateChannelBuffer ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Run the calibration of one DAC channel.

Note

Calibration runs about 2 ms per channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The DAC channel.

Return values :
  • HAL_OK – When HAL_OK the channel buffer has been calibrated correctly.

  • HAL_INVALID_PARAM – A parameter is invalid.

hal_status_t HAL_DAC_SetChannelBufferCalibrationValue ( hal_dac_handle_t * hdac , hal_dac_channel_t channel , uint32_t value )

Set a trimming offset value.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

  • value – DAC offset trimming value to be set. This parameter must be a number the range [1,31].

Return values :

HAL_OK

uint32_t HAL_DAC_GetChannelBufferCalibrationValue ( const hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Return the DAC trimming value.

Parameters :
  • hdac – DAC handle

  • channel – The selected DAC channel.

Return values :

Trimming – value in range [0, 31].

Separate channel mode configuration functions

group DAC_Exported_Functions_Group1_2

This section provides functions allowing to:

  • Configure the DAC channels in separate channel mode.

Functions

hal_status_t HAL_DAC_SetConfigChannel ( hal_dac_handle_t * hdac , hal_dac_channel_t channel , const hal_dac_channel_config_t * p_config )

Configure the selected DAC channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • p_config – DAC configuration structure.

  • channel – The selected DAC channel.

Return values :
  • HAL_OK – DAC instance has been correctly configured.

  • HAL_INVALID_PARAM – A parameter is invalid.

void HAL_DAC_GetConfigChannel ( const hal_dac_handle_t * hdac , hal_dac_channel_t channel , hal_dac_channel_config_t * p_config )

Get configuration for a channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

  • p_config – Pointer to a channel configuration structure parameters.

hal_status_t HAL_DAC_SetChannelAlignment ( hal_dac_handle_t * hdac , hal_dac_channel_t channel , hal_dac_data_alignment_t alignment )

Set the data width and alignment for the DAC channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

  • alignment – The alignment and data width: 12 bits right alignment, 12 bits left alignment, 8 bits right alignment.

Return values :

HAL_OK – DAC instance has been correctly configured.

hal_dac_data_alignment_t HAL_DAC_GetChannelAlignment ( const hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Get the data width and alignment for the DAC channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

Return values :

hal_dac_data_alignment_t – The data width and alignment for the DAC channel.

Separate channel mode, input and output operation functions

group DAC_Exported_Functions_Group2_1

This section provides functions allowing to:

  • Enable a “software trigger” conversion,

  • Start conversion in separate channel mode,

  • Stop conversion in separate channel mode,

  • Start conversion and enable DMA transfer in separate channel mode,

  • Stop conversion and disable DMA transfer in separate channel mode,

  • Set the data in holding register for DAC channel in separate channel mode,

  • Get the converted data in separate channel mode.

Functions

hal_status_t HAL_DAC_TrigSWConversionChannel ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

DAC channel software trigger conversion.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

Return values :

HAL_OK – or HAL_ERROR if software trigger is not enabled.

hal_status_t HAL_DAC_StartChannel ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Enable DAC and start conversion of channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

Return values :

HAL_OK

hal_status_t HAL_DAC_StopChannel ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Disable DAC and stop conversion of channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

Return values :

HAL_OK

hal_status_t HAL_DAC_SetChannelData ( hal_dac_handle_t * hdac , hal_dac_channel_t channel , uint32_t data )

Set the data holding register value for DAC channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

  • data – Data to be loaded in the selected data holding register.

Return values :

HAL_OK

uint32_t HAL_DAC_GetChannelData ( const hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Return the last data output value of the selected DAC channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

Return values :

The – selected DAC channel data output value.

hal_status_t HAL_DAC_SetChannelDMA ( hal_dac_handle_t * hdac , hal_dma_handle_t * hdma , hal_dac_channel_t channel )

Set the link between DAC channel and a DMA handler.

Parameters :
  • hdac – Pointer to hal_dac_handle_t structure.

  • hdma – Pointer to the DMA handler to be linked with the DAC channel.

  • channel – The selected DAC channel.

Return values :

HAL_OK

hal_status_t HAL_DAC_StartChannel_DMA ( hal_dac_handle_t * hdac , hal_dac_channel_t channel , const void * p_data , uint32_t size_byte )

Enable DAC and start conversion of channel with DMA.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

  • p_data – The source buffer address.

  • size_byte – The number of bytes of data to be transferred from memory to DAC peripheral

Return values :
  • HAL_OK – DAC instance has been correctly configured with the DMA.

  • HAL_ERROR – An internal inconsistency error or a parameter is invalid.

  • HAL_INVALID_PARAM – Invalid parameter.

  • HAL_BUSY – DMA channel state is active when calling this API.

hal_status_t HAL_DAC_StartChannel_DMA_Opt ( hal_dac_handle_t * hdac , hal_dac_channel_t channel , const void * p_data , uint32_t size_byte , uint32_t dac_opt_interrupt )

Enable DAC and start conversion of channel with DMA and optional interruption.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

  • p_data – The source buffer address.

  • size_byte – The number of bytes of data to be transferred from memory to DAC peripheral

  • dac_opt_interrupt – The DAC optional interrupt flag.

Return values :
  • HAL_OK – DAC instance has been correctly configured with the DMA.

  • HAL_ERROR – An internal inconsistency error or a parameter is invalid.

  • HAL_INVALID_PARAM – Invalid parameter.

  • HAL_BUSY – DMA channel state is active when calling this API.

hal_status_t HAL_DAC_StopChannel_DMA ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Disable DAC and stop conversion of channel with DMA.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

Return values :

HAL_OK – DMA successfully stopped.

Peripheral control functions

group DAC_Exported_Functions_Group3

This section provides functions allowing to set and configure the DAC main features:

  • the DMA double data mode,

  • the autonomous mode,

  • the configuration for the sample and hold mode,

  • the triangle wave addition,

  • the noise wave addition.

Functions

hal_status_t HAL_DAC_EnableChannelDMADoubleDataMode ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Enable the DAC DMA double data mode.

Parameters :
Return values :

HAL_OK.

hal_status_t HAL_DAC_DisableChannelDMADoubleDataMode ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Disable the DAC DMA double data mode.

Parameters :
Return values :

HAL_OK.

hal_dac_dma_double_data_mode_status_t HAL_DAC_IsEnabledChannelDMADoubleDataMode ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Check whether the DAC DMA double data mode is enabled.

Parameters :
Return values :

hal_dac_dma_double_data_mode_status_t – State of DMA double data mode:

  • HAL_DAC_DMA_DOUBLE_DATA_MODE_ENABLED

  • HAL_DAC_DMA_DOUBLE_DATA_MODE_DISABLED.

hal_status_t HAL_DAC_SetConfigChannelSampleAndHold ( hal_dac_handle_t * hdac , hal_dac_channel_t channel , const hal_dac_channel_sample_and_hold_config_t * p_config )

Set sample and hold configuration for a channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure that contains the configuration information for the specified DAC.

  • channel – The selected DAC channel.

  • p_config – Pointer to sample and hold mode structure parameters.

Return values :
  • HAL_OK – DAC Instance has been correctly configured.

  • HAL_ERROR – Internal timeout (to long time before writing in DAC_SHSRx has been completed).

  • HAL_INVALID_PARAM – If p_config is NULL.

void HAL_DAC_GetConfigChannelSampleAndHold ( const hal_dac_handle_t * hdac , hal_dac_channel_t channel , hal_dac_channel_sample_and_hold_config_t * p_config )

Get sample and hold configuration for a channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure that contains the configuration information for the specified DAC.

  • channel – The selected DAC channel.

  • p_config – Pointer to sample and hold structure parameters.

hal_status_t HAL_DAC_EnableChannelSampleAndHold ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Enable the DAC sample and hold mode for a channel.

Parameters :
Return values :

HAL_OK

hal_status_t HAL_DAC_DisableChannelSampleAndHold ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Disable the DAC sample and hold mode for a channel.

Parameters :
Return values :

HAL_OK

hal_dac_sample_and_hold_status_t HAL_DAC_IsEnabledChannelSampleAndHold ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Check whether the DAC sample-and-hold mode is enabled for a channel.

Parameters :
Return values :

hal_dac_sample_and_hold_status_t – Sample and hold state:

  • HAL_DAC_SAMPLE_AND_HOLD_ENABLED

  • HAL_DAC_SAMPLE_AND_HOLD_DISABLED.

hal_status_t HAL_DAC_EnableChannelAddingTriangleWave ( hal_dac_handle_t * hdac , hal_dac_channel_t channel , hal_dac_wave_amplitude_t amplitude )

Enable the DAC channel adding triangle wave.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The DAC channel.

  • amplitude – The triangle wave amplitude.

Return values :

HAL_OK

hal_status_t HAL_DAC_DisableChannelAddingTriangleWave ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Disable the DAC channel adding triangle wave.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The DAC channel.

Return values :

HAL_OK

hal_status_t HAL_DAC_EnableChannelAddingNoiseWave ( hal_dac_handle_t * hdac , hal_dac_channel_t channel , hal_dac_wave_amplitude_t amplitude )

Enable the DAC channel adding noise wave.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The DAC channel.

  • amplitude – Noise amplitude used for pseudo noise wave generation.

Return values :

HAL_OK

hal_status_t HAL_DAC_DisableChannelAddingNoiseWave ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Disable the DAC channel adding noise wave.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The DAC channel.

Return values :

HAL_OK

Callback functions and callback register functions

group DAC_Exported_Functions_Group4

This section is about callbacks functions:

  • The weak callbacks functions.

  • The register callback functions.

Functions

void HAL_DAC_ConvCpltCallback ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Conversion complete callback in non-blocking mode for the channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

void HAL_DAC_ConvHalfCpltCallback ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

Conversion half DMA transfer callback in non-blocking mode for the channel.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

void HAL_DAC_StopCpltCallback ( hal_dac_handle_t * hdac , hal_dac_channel_t channel )

DAC stop callback.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

void HAL_DAC_ErrorCallback ( hal_dac_handle_t * hdac )

DAC error callback.

Parameters :

hdac – Pointer to a hal_dac_handle_t structure.

hal_status_t HAL_DAC_RegisterConvCpltCallback ( hal_dac_handle_t * hdac , hal_dac_cb_t p_callback )

Register a user DAC callback to manage the completion conversion. To be used instead of the weak (overridden) predefined callback.

Parameters :
  • hdac – DAC handle

  • p_callback – Pointer to channel converter complete callback function.

Return values :
  • HAL_OK

  • HAL_INVALID_PARAM – In case of a NULL pointer for parameter.

hal_status_t HAL_DAC_RegisterConvHalfCpltCallback ( hal_dac_handle_t * hdac , hal_dac_cb_t p_callback )

Register a user DAC callback to manage the half completion conversion. To be used instead of the weak (overridden) predefined callback.

Parameters :
  • hdac – DAC handle

  • p_callback – Pointer to channel converter half complete callback function.

Return values :
  • HAL_OK

  • HAL_INVALID_PARAM – In case of a NULL pointer for parameter.

hal_status_t HAL_DAC_RegisterStopCpltCallback ( hal_dac_handle_t * hdac , hal_dac_cb_t p_callback )

Register a user DAC stop completed callback.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t

  • p_callback – Pointer to the DAC Stop completed callback

Return values :
  • HAL_OK – Operation completed successfully

  • HAL_INVALID_PARAM – Invalid parameter

hal_status_t HAL_DAC_RegisterErrorCallback ( hal_dac_handle_t * hdac , hal_dac_error_cb_t p_callback )

Register a user DAC callback to manage error. To be used instead of the weak (overridden) predefined callback.

Parameters :
  • hdac – DAC handle

  • p_callback – Pointer to the DAC channel error callback function.

Return values :
  • HAL_OK

  • HAL_INVALID_PARAM – In case of a NULL pointer for parameter.

Peripheral State, kernel clock frequency, IRQ and Errors functions

group DAC_Exported_Functions_Group5

This subsection provides functions allowing to:

  • Check the DAC state,

  • Check the DAC channels states,

  • Get the kernel clock frequency,

  • IRQ handler,

  • Check the DAC Errors.

Functions

hal_dac_state_t HAL_DAC_GetState ( const hal_dac_handle_t * hdac )

return the DAC handle state.

Parameters :

hdac – Pointer to a hal_dac_handle_t structure.

Return values :

hal_dac_state_t – DAC global state.

hal_dac_channel_state_t HAL_DAC_GetChannelState ( const hal_dac_handle_t * hdac , hal_dac_channel_t channel )

return the DAC channel state.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t structure.

  • channel – The selected DAC channel.

Return values :

hal_dac_channel_state_t – Channel state.

uint32_t HAL_DAC_GetClockFreq ( const hal_dac_handle_t * hdac )

Return the peripheral clock frequency for DAC.

Parameters :

hdac – Pointer to a hal_dac_handle_t structure.

Return values :

uint32_t – Frequency in Hz. 0 if the source clock of the DAC is not configured or not ready.

void HAL_DAC_IRQHandler ( hal_dac_handle_t * hdac )

Handle DAC interrupt request. This function is called when an interruption for DMA underrun error occurs.

Parameters :

hdac – Pointer to a hal_dac_handle_t structure.

uint32_t HAL_DAC_GetLastErrorCodes ( const hal_dac_handle_t * hdac )

Returns the last DAC error codes in a bits field.

Parameters :

hdac – Pointer to a hal_dac_handle_t structure.

Return values :

Last – error code, coded on an uint32_t bits field, it can be :

  • HAL_DAC_ERROR_NONE or a combinaison of the following values:

  • HAL_DAC_ERROR_DMA_UNDERRUN_CH1

  • HAL_DAC_ERROR_DMA_CH1

  • HAL_DAC_ERROR_DMA_UNDERRUN_CH2

  • HAL_DAC_ERROR_DMA_CH2

User Data API functions

group DAC_Exported_Functions_Group6

This subsection provides functions allowing to:

  • Set a user data pointer (ex: a user context) in a DAC handle,

  • Get a user data pointer (ex: a user context) from a DAC handle.

Note

A typical usage is to set user data pointer before starting a data conversion,

then retrieve it within the user conversion completion callback.

Functions

void HAL_DAC_SetUserData ( hal_dac_handle_t * hdac , const void * p_user_data )

Store user data pointer into the DAC handle.

Parameters :
  • hdac – Pointer to a hal_dac_handle_t.

  • p_user_data – Pointer to the user data.

const void * HAL_DAC_GetUserData ( const hal_dac_handle_t * hdac )

Retrieve user data pointer from the DAC handle.

Parameters :

hdac – Pointer to a hal_dac_handle_t.

Return values :
  • (void*) – the pointer to the user data, when previously set by HAL_DAC_SetUserData().

  • NULL – other way.

Dual channel mode configuration functions

Note

This section is not available for STM32C562xx.

Dual mode, input and output operation functions

Note

This section is not available for STM32C562xx.